package com.example.work.handler;

import com.example.work.entity.ErrorInfo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
/**
 * Description:    全局异常处理器
 * Author:         JinMu
 * CreateDate:     2018/3/5 22:41
 * Version:        1.0
 */
@ControllerAdvice
public class GlobalExecptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ErrorInfo<String> GlobalHandler(HttpServletRequest request, Exception e){
        ErrorInfo<String> errorInfo = new ErrorInfo<>();
        errorInfo.setCode(ErrorInfo.ERROR);
        errorInfo.setMessage(e.getMessage());
        errorInfo.setUrl(request.getRequestURL().toString());
        errorInfo.setData(null);
        return errorInfo;
    }
}